﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.WebApi.PackageTool
{
    /// <summary>
    /// ViewPackageWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ViewPackageWindow : Window
    {
        public ViewPackageWindow()
        {
            InitializeComponent();
        }

        private void _ctlBtnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "*.zip|*.zip";
            dlg.Multiselect = false;
            if(dlg.ShowDialog().Value == true)
            {
                _ctlEditPath.Text = dlg.FileName;

                using (Stream stream = CPPEI.WebApi.Packaging.PackageUtil.ReadFile(dlg.FileName, "/_module.xml"))
                {
                    CPPEI.WebApi.Packaging.ModuleDef module = CPPEI.WebApi.Packaging.ModuleDefDocument.Load(stream);
                    if(module!=null)
                    {
                        _ctlPanelModule.DataContext = module;
                        _ctlListRefs.ItemsSource = module.Refs;
                        _ctlListActions.ItemsSource = module.Actions;
                    }
                    else
                    {
                        MessageBox.Show("打开失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
        }
    }
}
